'use strict';
//define(['js/module.js','common'], function(controllers,common) {

	angular.module('baseinfo.settleCategoryList', [])
	.controller('settleCategoryListCtrl'
			,['$rootScope', '$scope', '$uibModal', 'BizData', '$ocLazyLoad', f])
	function f($rootScope, $scope, $uibModal, BizData, $ocLazyLoad) {

		var Model = this;

		/**
		name:bizData#onBeforeDelete
		description: <b>[回调型事件]</b>业务数据删除前
		@param {object} event 它的结构如下:<br/>
		{
			"source":组件的js对象
			,"cancel":可修改	，设置为true后中断当前delete动作
			,"deleteIDList":删除的行Id数组
		}
		*/
		Model.dataListBeforeDelete = function(event) {
//			alert(event.deleteIDList[0]);
		}
		/**
		name:bizData#onAfterDelete
		description: <b>[回调型事件]</b>业务数据删除后
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"deleteIDList" : 删除的行Id数组
		}
		</xmp>	
		*/
		Model.dataListAfterDelete = function(event) {
//			alert('删除之后！')
		}



		
		
		/**
		name:bizData#onIndexChanged
		description: <b>[回调型事件]</b>行记录变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"rowID" : 行Id,
			"rowIndex" : 行索引
		}
		</xmp>	
		*/
		Model.dataListIndexChanged = function(event) {

		}
		
		/**
		name:bizData#onDataChanged
		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
			"selfChanged" : 是否自身变化，false：从data触发的变化,
			"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
		}
		</xmp>	
		*/
		Model.dataListDataChanged = function(event) {
			
		}
		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"column" : 关系,
			"rowIndex" : 行索引,
			"value" : 新值,
			"originalValue" : 旧值
		}
		</xmp>	
		*/
		//输入值变化，监视input的输入，ng-change
		Model.dataListValueChanged = function(event) {

		}
		


		/**
		name:bizData#onBeforeNew
		description: <b>[回调型事件]</b>业务数据新增前
		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
		 */
		Model.dataListBeforeNew = function(event) {

		};
		/**
		 * name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
		 * 
		 * @param event
		 *            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
		 */
		Model.dataListAfterNew = function(event){
			
		}


		Model.gridRowClick = function(event){

		}

		Model.gridRowDblClick = function(event){
			Model.edit();
		}
		
		
		var options = {};
		options.container = Model;
		options.concept = 'SettleInfoOrg';
		options.name = 'dataList';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
   		options.creater = '/baseinfo/settleCategory/list/create';
		options.reader = '/baseinfo/settleCategory/list/query';
		options.writer = '/baseinfo/settleCategory/list/save';
		options.deleter = '/baseinfo/settleCategory/list/delete';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.defCols = {fID:{name:"fID",type:"String",label:""},settleCode:{name:"settleCode",type:"String",label:""},settleName:{name:"settleName",type:"String",label:""},sortNo:{name:"sortNo",type:"String",label:""},capitalID:{name:"capitalID",type:"String",label:""},capitalNo:{name:"capitalNo",type:"String",label:""},capitalName:{name:"capitalName",type:"String",label:""},capitalFullName:{name:"capitalFullName",type:"String",label:""},serverUrl:{name:"serverUrl",type:"String",label:""},appId:{name:"appId",type:"String",label:""},privateKey:{name:"privateKey",type:"String",label:""},format:{name:"format",type:"String",label:""},charset:{name:"charset",type:"String",label:""},alipayPublicKey:{name:"alipayPublicKey",type:"String",label:""},signType:{name:"signType",type:"String",label:""},returnUrl:{name:"returnUrl",type:"String",label:""},notifyUrl:{name:"notifyUrl",type:"String",label:""},cUsercode:{name:"cUsercode",type:"String",label:""},cUsername:{name:"cUsername",type:"String",label:""},cDatetime:{name:"cDatetime",type:"Datetime",label:""},userId:{name:"userId",type:"String",label:""},enterpriseNo:{name:"enterpriseNo",type:"String",label:""},enterpriseName:{name:"enterpriseName",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = 'fID';
		options.directDeleteMode = true;
		options.limit = 15;
		options.orderBy = 'sortNo ASC';
		
		options.onIndexChanging = null;
		options.onIndexChanged = Model.dataListIndexChanged;
		options.onBeforeNew = Model.dataListBeforeNew;
		options.onAfterNew = Model.dataListAfterNew;
		options.onBeforeDelete = Model.dataListBeforeDelete;
		options.onAfterDelete = Model.dataListAfterDelete;
		options.onValueChanged = Model.dataListValueChanged;
		options.onDataChanged = Model.dataListDataChanged;
		
		Model.dataList = new BizData(options);

		//监视对象中的所有变动
		Model.dataList.deregRows = $scope.$watch('Model.dataList.rows',Model.dataList.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
		//////////////////////////////////////////////
		
		
		////////////////////启动执行开始///////////////////////////////////////////////////
		//接收调用的参数
//		Model.params = $rootScope.$stateParams;  

		//刷新数据
	    Model.dataList.refreshData();
//	    console.log('Model.dataList.rows-->',Model.dataList.rows);

		////////////////////启动执行结束///////////////////////////////////////////////////
			




		//添加(调用对话框)
	    Model.add = function() {
			//打开对话框
			var options = {};
			
			options.recordState = 'new';
			options.id = '';

	        var modalInstance = $uibModal.open({  
	        	backdrop: 'static',
	        	//size: 'lg',
	            //templateUrl: $rootScope.getProjectName() + '/baseinfo/settleCategory/single',  
	        	templateUrl: $rootScope.getProjectName() + $rootScope.viewprefix + '/baseinfo/settleCategory/single' + $rootScope.viewsuffix,  
	            controller: 'settleCategorySingleCtrl as Model',  
	            resolve: {
	                params: function () {  
	                    return angular.copy(options);  
	                }
                    ,title : function() { return '新建'; }
                    ,loadCtrl : $ocLazyLoad.load('js/baseinfo/settleCategory/single')
		        }
	        });  
	        //模态窗口打开之后执行的函数
	        modalInstance.opened.then(function(){  
	        });  
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (result) {  
	             //成功关闭窗口
	             Model.dataList.refreshPageData();
	        }, function (reason) {  
	        });  
				
			
			
		};

		//修改
		Model.edit = function() {
			//打开对话框
			var index = Model.dataList.currentIndex;
			var row = Model.dataList.rows[index];

			var options = {};
			
			options.recordState = 'edit';
			options.id = row.fID.value;

	        var modalInstance = $uibModal.open({  
	        	backdrop: 'static',
	        	//size: 'lg',
	            //templateUrl: $rootScope.getProjectName() + '/baseinfo/settleCategory/single',  
	        	templateUrl: $rootScope.getProjectName() + $rootScope.viewprefix + '/baseinfo/settleCategory/single' + $rootScope.viewsuffix,  
	            controller: 'settleCategorySingleCtrl as Model',  
	            resolve: {
	                params: function () {  
	                    return angular.copy(options);  
	                }
                    ,title : function() { return '修改'; }
                    ,loadCtrl : $ocLazyLoad.load('js/baseinfo/settleCategory/single')
		        }
	        });  
	        //模态窗口打开之后执行的事件
	        modalInstance.opened.then(function(){
	        });  
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (result) {  
	             //成功关闭窗口
	             Model.dataList.refreshPageData();
//	             Model.dataList.currentIndex = index;
	        }, function (reason) {  
	        });  
				
		};


		//（删除）
		Model.remove = function(){
			var ids = new Array();
			for(var index=0;index<Model.dataList.rows.length;index++){
				if(Model.dataList.rows[index].userdata.checked){
					ids.push(Model.dataList.rows[index].fID.value);
				}
			}
			if(ids.length<=0){
            	$rootScope.addAlert('info','请选择需要删除的行。');
            	return;
			}
			Model.dataList.deleteData(ids);
			Model.dataList.refreshData();
		}

		Model.refresh = function(){
			Model.dataList.refreshData();
		}

		//页面渲染完成事件（页面元素加载完成）
	    $scope.load = function(){
	    	console.log('load!','页面渲染完成事件！');
	    };

	    //表格标题中的选择框ng-model
	    $scope.checkboxValue = false;
	    //表格标题中的选择对话框click事件
	    Model.checkboxClick = function () {  
			for(var index=0;index<Model.dataList.rows.length;index++){
				Model.dataList.rows[index].userdata.checked=$scope.checkboxValue;
			}
	    };    


		return Model;
		
		
	}	
	
//});
